package com.yanxu.lock.reentrantlock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025/3/16-2025
 * @Description: 演示 多线程电影院预定座位
 */
public class CinemaBookSeat {

    static Lock lock = new ReentrantLock();

    private static void bookSeat() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + " 开始预定座位!!!");
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + " 完成预定座位!!!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        new Thread(() -> bookSeat(), "thread1").start();
        new Thread(() -> bookSeat(), "thread2").start();
        new Thread(() -> bookSeat(), "thread3").start();
        new Thread(() -> bookSeat(), "thread4").start();
    }
}